package com.wzt.base.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * @author Zenith
 */
public class SendSmsUtil {
    private static final String UID = "lFDmRyEDuk";
    private static final String KEY = "4108CE4E28751F471C90835B0A875001";

    /**
     * 发送短信
     *
     * @param phones  手机们 a,b
     * @param content 发送内容
     * @return 返回值
     */
    public static String sendSms(String phones, String content) {
        PostMethod post = null;
        try {
            HttpClient client = new HttpClient();
            post = new PostMethod("http://utf8.api.smschinese.cn");
            //在头文件中设置转码
            post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf8");
            NameValuePair[] data = {new NameValuePair("Uid", UID),
                    new NameValuePair("Key", KEY),
                    new NameValuePair("smsMob", phones),
                    new NameValuePair("smsText", content)};
            post.setRequestBody(data);

            client.executeMethod(post);
            int statusCode = post.getStatusCode();
            //200 404 400
            System.out.println("statusCode:" + statusCode);
            String result = new String(post.getResponseBodyAsString().getBytes("utf8"));
            return result;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (post != null) {
                post.releaseConnection();
            }
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(SendSmsUtil.sendSms("17345165728", "您的验证码为：1412"));
    }
}
